<?php
namespace Admin\Model;
use IDCU\Model\RelationModel;

class FinanceModel extends RelationModel
{
    protected $connection = array(
        'db_type'       =>  'mysql',
        'db_host'       =>  '114.55.30.131',
        'db_port'       =>  '3306',
        'db_name'       =>  'wxsmart',
        'db_user'       =>  'zhkj_crm',
        'db_pwd'        =>  'AdkdCGP7fHcjht5c',
        'db_charset'    =>  'utf8',
        'db_debug'      =>  true,
    );
    protected $tablePrefix = '';
    protected $tableName = 'tp_fruit_order';
    
    public function fruitOrder($where=array(), $pageSize=20){
        $model = $this;
        $where['isdel'] = array('neq',1);
        $key = I('key');
        if($key){
            if(preg_match("/^\d+$/", $key))
                $where['tel'] = array('like',"%$key%");
            else
                $where['name'] = array('like',"%$key%");
        }
        /************************************* 翻页 ****************************************/
        $count = $model->where($where)->count();
        $page = new \ZL\Page($count, $pageSize, C('PAGE_01'));
        $data['page'] = $page->show();
        /************************************** 取数据 ******************************************/
        $orderData = $model->where($where)->order('time desc')->limit($page->limit.','.$page->listRows)->select();
        foreach ($orderData as &$value) {
            $value['order_type_title'] = '水果';
            $value['order_type'] = 1;
        }
        $data['title'] = '账单列表';
        $data['th'] = array('#', '账单号', '创建时间', '客户姓名', '联系方式', '账单类型', '金额', '支付方式', '操作');
        $data['data'] = $orderData;
        $data['type'] = 1;
//        myDump($where);die;
        return $data;
    }

//    public function shelpOrder($where=array(), $pageSize=20){
//        $model = M('Qtorder','tp_','DB_WX');
//        $key = I('key');
//        if($key){
//            if(preg_match("/^\d+$/", $key))
//                $where['tel'] = array('like',"%$key%");
//            else
//                $where['name'] = array('like',"%$key%");
//        }
//        /************************************* 翻页 ****************************************/
//        $count = $model->where($where)->count();
//        $page = new \ZL\Page($count, $pageSize);
//        $data['page'] = $page->show();
//        /************************************** 取数据 ******************************************/
//        $orderData = $model->where($where)->order('time desc')->limit($page->limit.','.$page->listRows)->select();
//        foreach ($orderData as &$value) {
//            $value['order_type_title'] = $v[];
//            $value['order_type'] = 1;
//        }
//        $data['title'] = '账单列表';
//        $data['th'] = array('#', '账单号', '创建时间', '客户姓名', '联系方式', '账单类型', '金额', '操作');
//        $data['data'] = $orderData;
//        $data['type'] = 1;
//        return $data;
//    }

    public function ExpenseOrder($pageSize=20,$limit=false){
        $model = D('Assistant/Account');
        $logData = $model->searchLog($pageSize,$limit);
        $type = D('Admin/Dict')->getDict('payWay',1);
        foreach ($logData['data'] as &$value) {
            $value['order_type_title'] = '物业费账单';
            $value['pay_way'] = $type[$value['pay_way']];
            $value['order_type'] = 2;
            $value['room_name'] = $value['full_parent_name'].$value['name'];
            $value['accountDetail'] = $model->excelDtail($value['account_id']);
        }
        $data['title'] = '物业费缴费列表';
        $data['th'] = array('#', '账单号', '支付时间', '缴费人姓名', '户主联系方式', '支付方式', '金额', '地址', '备注', '操作');
        $data['data'] = $logData['data'];
        $data['page'] = $logData['page'];
        $data['type'] = 2;
//        myDump($data);die;
        return $data;
    }

    public function getList($type=false, $status=false, $pageSize=10){
        $start_time = I('start_time');
        $end_time = I('end_time');
        if ($start_time)
            $start_time = strtotime($start_time);
        if ($end_time)
            $end_time = strtotime($end_time);
        if ($start_time && !$end_time)
            $where['time'] = array('egt',$start_time);
        elseif (!$start_time && $end_time)
            $where['time'] = array('elt',$end_time);
        elseif ($start_time && $end_time)
            $where['time'] = array('BETWEEN',"$start_time,$end_time");

        if ($status)
            $where['isuse'] = array('in','%'.$status.'%');
        if (!$type)
            $type = I('type');
        if ($type) {
            if ($type==1) {
                return $this->fruitOrder($where, $pageSize);
            }elseif ($type==2){
                return $this->ExpenseOrder($pageSize);
            }elseif ($type==3){
                return $this->shelpOrder($where, $pageSize);
            }
        }
        else
            return $this->fruitOrder($where,$pageSize);
    }

    public function detail($id,$type){
        if($type==1){
            $data = $this->find($id);
            $data = $this->intToName($data);
//            echo '<pre>';
//            var_dump($data);die;
            $data['order_type'] = '水果';
        }

        return $data;
    }

    public function intToName($data){
        foreach ($data as $k=>$v){
            $_data[$k] = $v;
            //转中文 是否退款
            $has = strpos($k, 'isrefund');
            if ($has!==false) {
                switch ($v) {
                    case 0:
                        $_data[$k] = '未退款';
                        break;
                    case 1:
                        $_data[$k] = '提交退款';
                        break;
                    case 2:
                        $_data[$k] = '已退款';
                        break;
                    default:
                        $_data[$k] = '无';
                        break;
                }
            }
            //转中文 是否发货
            $has = strpos($k, 'isuse');
            if ($has!==false) {
                switch ($v) {
                    case 0:
                        $_data[$k] = '未发货';
                        break;
                    case 1:
                        $_data[$k] = '已发货';
                        break;
                    case 2:
                        $_data[$k] = '已收货';
                        break;
                    default:
                        $_data[$k] = '无';
                        break;
                }
            }
            //转中文 商品
            $has = strpos($k, 'info');
            if ($has!==false) {
                $info = unserialize($v);
                $_data[$k] = '';
                foreach ($info as $k1=>$v1){
                    $_data[$k] .= ' 商品：'.$v1['name'].' 数量：'.$v1['num'].' 总价：'.$v1['price'];
                }
            }
        }
        return $_data;
    }
    protected function _after_select(&$result, $options)
    {
        parent::_after_select($result, $options); // TODO: Change the autogenerated stub
    }

}